import org.bytedeco.ffmpeg.avcodec.AVPacket;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.FrameRecorder;

public class FileSlicing {
    public static void main(String[] args) throws FrameGrabber.Exception, FrameRecorder.Exception {
        //原视频文件
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("C:\\Users\\Lenovo\\Desktop\\ddd\\ww.mp4");
        grabber.start();

        //输出目录
        String targetFolder = "C:\\Users\\Lenovo\\Desktop\\ddd";

        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(targetFolder+"\\index.m3u8", grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels());
        recorder.setFormat("hls");
        recorder.setOption("hls_time", "10");
        recorder.setOption("hls_playlist_type", "vod");
        recorder.setOption("hls_list_size", "0");
        recorder.setOption("hls_segment_filename", targetFolder+"\\video_%04d.ts");
        recorder.start(grabber.getFormatContext());

        AVPacket packet = null;
        while ((packet = grabber.grabPacket()) != null) {
            recorder.recordPacket(packet);
            avcodec.av_packet_unref(packet);
        }

        recorder.close();
        grabber.close();
    }
}
